#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10, M = 2e5 + 10;
int h[N], e[M], ne[M], idx;
int q[N];
int d[N];
int pos[N];
int k;
int n, m;
struct Node
{
    int a, b;
}edge[N];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool topsort()
{
    int hh = 0, tt = -1;
    for (int i = 1; i <= n; i++)
        if (!d[i])
            q[++tt] = i;
    while (hh <= tt)
    {
        int t = q[hh++];
        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (--d[j] == 0)
                q[++tt] = j;
        }
    }
    return tt == n - 1;
}

int main()
{
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        memset(h, -1, (n + 1) * 4);
        memset(d, 0, (n + 1) * 4);
        idx = k = 0;
        while (m--) {
            int t, a, b;
            cin >> t >> a >> b;
            if (!t) {
                edge[k++] = { a,b };
            }
            else {
                add(a, b);
                d[b]++;
            }
        }
        if (!topsort()) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
            for (int i = 1; i <= n; i++) {
                for (int j = h[i]; ~j; j = ne[j]) {
                    cout << i << " " << e[j] << endl;
                }
            }
            for (int i = 0; i < n; i++) {
                pos[q[i]] = i;
            }
            for (int i = 0; i < k; i++) {
                int a = edge[i].a;
                int b = edge[i].b;
                if (pos[a] > pos[b])swap(a, b);
                cout << a << " " << b << endl;
            }
        }
    }
    return 0;
}